﻿#--------------------
# 删除指定目录组中超过时限的文件,不包含文件夹
#--------------------

function getAllFilesNeedRemoved ($dirs_needRemoveFile)
{
    $allFileInfos = @();
    foreach ($item in $dirs_needRemoveFile)
    {
        $allFileInfos +=  Get-ChildItem -Path $item -Force -Recurse -ErrorAction SilentlyContinue;
    }

    return $allFileInfos;
}

function removeFiles ($allFileInfos_needRemove,$timeDeltaByNow)
{
    foreach ($item in $allFileInfos_needRemove)
    {
       $now = Get-Date
       $timeDelta =$now  -  $item.LastWriteTime ;
       if ( $timeDelta.Days -gt  $timeDeltaByNow)
       {
           Remove-Item $item.FullName ;
           
       }
    }
}

$dirPath_needRemoveFiles = ,"E:\src\BDCenter.Manager\BDCenter.Manager\bin\Debug123\Debug"; # 单元素数组

$allFileInfos = getAllFilesNeedRemoved $dirPath_needRemoveFiles
removeFiles $allFileInfos 10  #删除迄今为止超过10天的文件,目录被保留


